package com.qf.jobdemo2.handler;

import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Component
public class XxlJobHandler {

    @XxlJob("task1")
    public void task1(){ //0,1
        //这个对象可以获取分片索引、分片总数、任务参数
//        XxlJobHelper

        System.out.println("--任务执行--");

        List<Integer> ids = tableIds();

        //获取分片总数
        int shardTotal = XxlJobHelper.getShardTotal();
        //当前任务对应的分片索引
        int shardIndex = XxlJobHelper.getShardIndex();

        for (Integer id : ids) {
            if (id % shardTotal == shardIndex){
                //执行任务
                System.out.println("调用时间: " + LocalDateTime.now());
                System.out.println("分片index: " + shardIndex + "执行的任务数据表id：" + id);

                System.out.println("-------------------------");
            }
        }
    }


    /**
     * 模拟数据库表中的记录id，id的值为 1-100
     * @return
     */
    public List<Integer> tableIds(){
        List<Integer> list = new ArrayList<>();
        for (int i = 1; i <= 100; i++) {
            list.add(i);
        }
        return list;
    }
}
